// 奖项名数组
//TODO
let prizesArr = ["HUAWEI P60 Art", "HUAWEI Nova 10 Pro 4G", "HUAWEI 畅享 60 Pro", "Matebook E 2023", "MatePad Air", "HUAWEI Watch D", "HUAWEI FreeBuds 5", "720全效空气净化器1i 白色"];

// 奖项图片数组
//TODO
let arrBigImg = ["./img/1.png", "./img/2.png", "./img/3.png", "./img/4.png", "./img/5.png", "./img/6.png", "./img/7.png", "./img/8.png", "./img/9-prizes.png"];

// 获取所有奖励单元格
//TODO
let allPrizesLi = document.querySelectorAll('.prizes-li');

// 获取所有图片
//TODO
let prizesImg = document.querySelectorAll('.pic');

// 旋转的初始值
//TODO
let count = 0;
let isClick = true;
let index = 3;

// 转到哪个位置
//TODO
let prizesPosition = 0;

// 绑定 IMG
//TODO
for (let j = 0;j < prizesImg.length; j++) {
    prizesImg[j].src = arrBigImg[j];
}

// 转速。值越大，速度越慢
//TODO
let speed = 500;

// 旋转功能
//TODO
function roll() {
    // 速度衰减
    //TODO
    speed -= 50;
    if (speed <= 10) {
        speed = 10;
    }

    // 删除每个调用的所有活动类名称
    //TODO
    for (let j = 0; j < allPrizesLi.length; j++) {
        allPrizesLi[j].classList.remove('active-li');
    }
    prizesPosition++;

    // 计算匝数
    //TODO
    if (prizesPosition >= allPrizesLi.length - 1) {
        prizesPosition = 0;
        count++;
    }
    allPrizesLi[prizesPosition].classList.add('active-li');
    let initSpeed = 500;
    let timer;
    // 至少总转数
    let totalCount = 5;

    // 当满足圈数和指定位置时停止
    //TODO
    if (count >= totalCount && (prizesPosition + 1) === index) {
        clearTimeout(timer);
        isClick = true;
        speed = initSpeed;
        //等待 1 秒打开弹窗
        timer = setTimeout(openDialog, 1000);
    } else {
        // 等待 1 秒打开弹窗
        timer = setTimeout(roll, speed);
        // 最后一圈减速
        if (count >= totalCount - 1 || speed <= 50) {
            speed += 100;
        }
    }
}

// 绘制开始功能
//TODO
function startDraw() {
    // 防止多次触发抽奖
    if (isClick) {
        count = 0;
        // 随机生成获胜位置
        index = Math.floor(Math.random() * prizesArr.length + 1);
        roll();
        isClick = false;
    }
}

//openDialog函数
//TODO
function openDialog() {
    linkObj.messageFromHtml(prizesArr[prizesPosition]);
}